package com.sofwin;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.sofwin.interceptor.LoginInterceptor;
import com.sofwin.system.service.UserService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @packageName: com.sofwin
 * @user: andyliu
 * @date: 2021/7/9 9:58
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
@EnableWebMvc // mvc:annotaion-driven
@ComponentScan("com.sofwin")
public class MvcConfiger implements WebMvcConfigurer {

    @Autowired
    private UserService userService;

    @Bean
    public LoginInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }
    @Bean
    public Config getConfig(){
        Properties prop = new Properties();
        prop.setProperty("kaptcha.border","yes");// 设置是否有边框
        prop.setProperty("kaptcha.border.color","105,179,90"); // 边框颜色
        prop.setProperty("kaptcha.textproducer.font.color","blue");// 字体颜色
        prop.setProperty("kaptcha.image.width","100");
        prop.setProperty("kaptcha.image.height","32");
        prop.setProperty("kaptcha.textproducer.font.size","30");
        prop.setProperty("kaptcha.textproducer.char.length","4");
        prop.setProperty("kaptcha.textproducer.font.names","楷体楷体,微软雅黑");
        prop.setProperty("kaptcha.noise.color","yellow");
        Config config = new Config(prop);
        return config;
    }
    @Bean
    public DefaultKaptcha getKaptcha(){
        DefaultKaptcha kaptcha = new DefaultKaptcha();

        kaptcha.setConfig(getConfig());
        return kaptcha;

    }

    // <mvc:interceptors>
    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login").excludePathPatterns("/bootstrap/**").excludePathPatterns("/verification");
    }
  // <mvc:resources>
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/");
    }




//    // <mvc:default-servlet-handler>
//    // 配置静态资源
//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();// <mvc:default-servlet-handler>
//    }

    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver getResourceViewResolver(){
        InternalResourceViewResolver resolver= new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        return  resolver;
    }
    // 多媒体视图解析器
    @Bean
    public CommonsMultipartResolver getMultipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(10240000);
        return resolver;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(getSecurityManager());
        bean.setLoginUrl("/login.jsp");// 指定登陆页面     该页面不进行安全验证，当安全验证失败后自动跳转到指定url
        bean.setSuccessUrl("/index");// 设置身份认证成功后自动跳转的url   可以省略
        Map map = new HashMap<>();
        map.put("/bootstrap/**","anon");// key 资源访问路径  value 代表是否放行  anon  不进行权限验证 authc 需要进行权限验证
        map.put("/verification","anon");
        map.put("/**","authc");
        map.put("/user/login","anon");
        bean.setFilterChainDefinitionMap(map);// 定义哪些请求进行认证器    哪些请求直接放行
        return bean;
    }
    @Bean
    public DefaultWebSecurityManager getSecurityManager(){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        // 设置用户自定义域
        manager.setRealm(getUserReal());
        return manager;
    }

    @Bean
    public UserRealm getUserReal(){
        UserRealm realm=new UserRealm();
        realm.setUserService(userService);
        return realm;
    }



}
